<?php

	/*
		Constant
	*/
	
	$numRequired = 10;	// num of files required for each kind of data, can change
	$timeInterval = 60*60;	// change time interval
	
	// change the base url if host is changed
	$data_baseURL = "http://localhost/~mingxuanli/syd-traffic/server_program/server.php";
	// following urls will be changed automatically
	$incidentURL = "../traffic-data/Incidents";
	$F3URL = "../traffic-data/F3";
	$M4URL = "../traffic-data/M4";
	$M7URL = "../traffic-data/M7";
	
	
	/*
		Variables
	*/
	
	$numIncident;
	$numF3;
	$numM4;
	$numM7;
	
	$startTime;
	$endTime;
	
	$request_type;
	$flag;
	
	function myprint($str){
		echo $str."<br>";
	}
	
	function countNumFilesOfDir($url){
		$filecount = 0;
		$dir = opendir($url);
		if(!$dir){
			return -1;
		}
		while (($file = readdir($dir)) !== false) {
        	if ($file[0] == '.'){
        		continue; 
			}
        	if (is_dir($url.$file)){       
            	$filescount += filecount($FolderPath.$file.DIRECTORY_SEPARATOR);                       
        	}else {
            	$filecount++;
        	}
   		}   
    	closedir($dir);
     	return $filecount;
	}
	
	function findFiles($url, $interval){
		$dir = opendir($url);
		if(!$dir){
			return -1;
		}
		$count = 0;
		$retArr = array();
		while(($file = readdir($dir)) !== false){
			if ($file[0] == '.'){
        		continue; 
			}
			if (is_dir($url.$file)){
				                             
        	}else {
            	if($count%$interval == 0){
            		array_push($retArr,$file);
            	}
            	$count++;
        	}
		}
		closedir($dir);
		return $retArr;
	}
	
	function countFiles(){
		global $numIncident, $numF3, $numM4, $numM7, $incidentURL, $F3URL, $M4URL, $M7URL;
		$numIncident = countNumFilesOfDir($incidentURL);
		$numF3 = countNumFilesOfDir($F3URL);
		$numM4 = countNumFilesOfDir($M4URL);
		$numM7 = countNumFilesOfDir($M7URL);
	}
	
	// open directory incidents to get all the files
	// create and return array of selected incidents
	function openIncidents(){
		global $numIncident, $numRequired, $incidentURL;
		$interval = floor($numIncident/$numRequired);
		$arrayOfFiles = findFiles($incidentURL,$interval);
		$retArr;
		
		return $arrayOfFiles;
		//print_r($arrayOfFiles);	
	}
	
	// same as incidents but deprecated in this server
	function openF3(){
		global $numF3, $numRequired, $F3URL;
		$interval = floor($numF3/$numRequired);
		$arrayOfFiles = findFiles($F3URL,$interval);
		//print_r($arrayOfFiles);	
	}
	
	function openM4(){
		global $numM4, $numRequired, $M4URL;
		$interval = floor($numM4/$numRequired);
		$arrayOfFiles = findFiles($M4URL,$interval);
		
	}
	
	function openM7(){
		global $numM7, $numRequired, $M7URL;
		$interval = floor($numM7/$numRequired);
		$arrayOfFiles = findFiles($M7URL,$interval);
		//print_r($arrayOfFiles);	
	}
	
	function fileToTime($filename){
		$strArr = explode(".",$filename);
		return makeTime(end($strArr));
	}
	
	function makeTime($timeString){
		$strArr = explode("_",$timeString);
		$date = (int) $strArr[0];
		$time = (int) $strArr[1];
		$hour = floor($time/10000);
		$minute = floor(($time-$hour*10000)/100);
		$second = ($time-$hour*10000-$minute*100);
		$year = floor($date/10000);
		$month = floor(($date-$year*10000)/100);
		$day = ($date-$year*10000-$month*100);
		return mktime($hour,$minute,$second,$month,$day,$year);
	}
	
	function searchFilesOnTime($url,$startTime,$endTime){
		$dir = opendir($url);
		if(!$dir){
			return -1;
		}
		$retArr = array();
		while (($file = readdir($dir)) !== false) {
        	if ($file[0] == '.'){
        		continue; 
			}
        	if (is_dir($url.$file)){       
        	                     
        	}else {
        		$fileTime = fileToTime($file);
        		if($fileTime >= $startTime && $fileTime <= $endTime){
            		array_push($retArr,$file);
        		}
        	}
   		}   
    	closedir($dir);
     	return $retArr;
	}
	
	// get the url and assign values
	$request=$_GET["type"];
	$flag=$_GET["flag"];
	$time=$_GET["time"];
	$file=$_GET["file"];
	
	date_default_timezone_set('Australia/Sydney');
	
	// executions below:
	if($flag == "debug"){
		echo "Debug mode:"."<br>";
		echo "Incident files : ".countNumFilesOfDir($incidentURL)."<br>";
		echo "F3 files: ".countNumFilesOfDir($F3URL)."<br>";
		echo "M4 files: ".countNumFilesOfDir($M4URL)."<br>";
		echo "M7 files: ".countNumFilesOfDir($M7URL)."<br><br>";
	}
	if($request == "incidents"){
		countFiles();
		echo json_encode(openIncidents());
		//echo openIncidents()."<br>";
	}else{
		//$startTime = makeTime($time);
		$startTime = $time;
		$endTime = $startTime + $timeInterval;
		if($request == "getStartTime"){
			$startTime = fileToTime($file);
			echo $startTime;
			return;
		}else if($request == "getIncidentsWithinOneHour"){
			$arrFiles = searchFilesOnTime($incidentURL,$startTime,$endTime);
		}else if($request == "getF3"){
			$arrFiles = searchFilesOnTime($F3URL,$startTime,$endTime);
			//echo json_encode(end($arrFiles));
		}else if($request == "getM4"){
			$arrFiles = searchFilesOnTime($M4URL,$startTime,$endTime);
			//echo json_encode(end($arrFiles));
		}else if($request == "getM7"){
			$arrFiles = searchFilesOnTime($M7URL,$startTime,$endTime);
			//echo json_encode(end($arrFiles));	
		}
		echo json_encode($arrFiles);
		//print_r($arrFiles);
	}
?>